#!/bin/bash
#2020年3月4日10:41:48
#auto backup mysql db
#by author www.jfedu.net
########################
SQL_DB="$*"
SQL_USR="backup"
SQL_PWD="bak123456"
SQL_CMD="/usr/bin/mysql"
SQL_DIR="/data/backup/`date +%F`"
if [ $# -eq 0 ];then
	echo -e "\033[32m--------------------\033[0m"
	echo -e "\033[32mUsage:{/bin/sh $0 jfedu001|jfedu002 jfedu003|help}\033[0m"
	exit 1
fi

if [ ! -d $SQL_DIR ];then
	mkdir -p $SQL_DIR
	echo "The $SQL_DIR create succeeded."
fi
${SQL_CMD} -e "grant select,lock tables on *.* to $SQL_USR@'localhost' identified by \"$SQL_PWD\";"
for SQL_DB in $(echo $SQL_DB)
do
	${SQL_CMD}dump -u$SQL_USR -p$SQL_PWD ${SQL_DB} > $SQL_DIR/${SQL_DB}.sql
	if [ $? -eq 0 ];then
		echo -e "\033[32m--------------------\033[0m"
		echo -e "\033[32mThe ${SQL_DB} backup succeeded.\033[0m"
		echo "$SQL_DIR/"
		ls -l $SQL_DIR/
	else
		echo -e "\033[32m--------------------\033[0m"
		echo -e "\033[32mThe ${SQL_DB} backup falied.\033[0m"
		echo "$SQL_DIR/"
		rm -rf $SQL_DIR/${SQL_DB}.sql
	        ls -l $SQL_DIR/
	fi
done